
var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;
layui.use(['tableEdit',"excel","selectInput"], function () {
    let tableEdit = layui.tableEdit;
    let layer = layui.layer,
        excel = layui.excel,
        selectInput = layui.selectInput;

    lay('.starttime').each(function () {
        laydate.render({
            elem: this
        });
    });

    function getAllJson(jsons) {
        let tmpitem = []
        for (let item of jsons) {
            let tmparr = {}
            tmparr["value"] = item["code"];
            tmparr["name"] = item["name"];
            if (item["users"]) {
                for (let it of item["users"]) {
                    item["list"].push(it)
                }
            }
            if (item["list"] && item["list"].length) {
                tmparr["children"] = getAllJson(item["list"]);
                tmpitem.push(tmparr)
            } else {
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }

    function getxmselect(el, name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon: true,
                indent: 20,
                expandedKeys: false
            },
            height: 'auto',
            data: data,
            on: function ({arr, change, isAdd}) {
                return this
            }
        })
    }


    layer.ready(function () {
        //获取当前用户的部门及人员结构树
        let treeList = getCurrentDepartmentUser();

        if (treeList && treeList.list) {
            let userTree = getAllJson(treeList.list);
            getxmselect("#operationCode", "operationCode", userTree);
        } else {
            getxmselect("#operationCode", "operationCode", [{name: user.name, value: user.code}])
        }

        //供应商
        $.ajax({
            url: "customer2/getAllInspectUnit",
            success: function (res) {
                if (res.status == 200) {
                    $.each(res.data, function (index, item) {
                        $("#supplierCode").append(new Option(item.name, item.code));
                    });
                    form.render('select');
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
            }
        });

    });
    form.render();//表单刷新

    let initData = [];
    table.render({
        elem: '#collection6Select'
        , data: initData
        , where: {operationCode: user.code}
        , url: 'receivePayment/collectionStatistics6ListByPage'
        , toolbar: '#collection6Toolbar'
        ,totalRow: true
        , cols: [[
              {field: 'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'}
            , {field: 'billNo', title: '外销合同号', width: 200}
            , {field: 'customerName', title: '客户名称', width: 200}
            , {field: 'customerCode', title: '客户编码', width: 120}
            , {field: 'sourceOfCustomers', title:'客户来源',width: 200}
            , {field: 'productCode', title:'产品编码',width: 200}
            , {field: 'specificationCode', title:'规格编码',width: 200}
            , {field: 'signingDateStr', title: '签约日期', sort: true,width: 200}
            , {field: 'businessDeliveryDateStr', title: '业务要求交货日期', sort: true, width: 150}
            , {field: 'unitPrice', title: '单价', sort : true, width: 150}
            , {field: 'contractMoneySubtotal', title: '外销金额小计', sort: true, width: 150, totalRow: true}
            , {field: 'createTimeStr', title: '采购日期', sort: true, width: 200}
            , {field: 'purchaseOrderCode', title: '采购单号', width: 200}
            , {field: 'supplierName', title: '供应商名称', width: 200}
            , {field: 'contractNum', title: '外销数量', sort: true, width: 150, totalRow: true}
            , {field: 'num', title:'数量', sort : true, width: 150, totalRow: true}
            , {field: 'productPrice', title: '产品单价', width: 150, totalRow: true}
            , {field: 'productCost', title: '产品费用', width: 150, totalRow: true}
            , {field: 'purchaseCost', title: '采购成本', sort: true, width: 150, totalRow: true}
            , {field: 'productPriceSubtotal', title: '产品单价小计', sort: true, width: 150, totalRow: true}
            , {field: 'purchaseCostSubtotal', title: '采购成本小计', sort: true, width: 150, totalRow: true}
            , {field: 'deliveryDateStr', title: '要求交货日期', sort: true, width: 200}
            , {field: 'feedbackDeliveryDateStr', title: '供应商反馈交期', sort: true, width: 200}
            , {field: 'actualDeliveryDateStr', title: '实际交期', sort: true, width: 200}
            , {field: 'carryPlanNo', title: '出运计划号', width: 200}
            , {field: 'planAmount', title: '出运计划数量', sort: true, width: 150, totalRow: true}
            , {field: 'lastAmountOfPlan', title: '计划剩余数量', sort: true, width: 150, totalRow: true}
            , {field: 'carryInvoiceNo', title: '出运发票号', width: 200}
            , {field: 'carryDetailNum', title: '出运明细数量', sort: true, width: 150, totalRow: true}
            , {field: 'lastAmountOfDetail', title: '出运明细剩余数量', sort: true, width: 150, totalRow: true}
            , {field: 'invoiceNo', title: '结汇发票号', width: 200}
            , {field: 'billExchangeInfoNum', title: '结汇数量', sort: true, width: 150, totalRow: true}
            , {field: 'lastAmountOfBillExchange', title: '结汇剩余数量', sort: true, width: 150, totalRow: true}
        ]]
        , id: 'collectionTableId'
        , height: $(document).height() - $('#collection6Select').offset().top - 50
        , page: true
        , limit: 300
        , limits: [300, 600, 900]
    });


    form.on('submit(submitform)', function (data) {
        table.reload('collectionTableId', {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: data.field
        }, 'data');
        return false;
    });

    form.on('submit(exportData)', function (queryReq) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: queryReq.field,
            url:"receivePayment/collectionStatisticsSixExport",
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 80,
                        'X': 80
                    }, 80);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '产品出货详情.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });

});